
from google.appengine.ext import db
import logging

def ReturnDBTextOfStr(strVal):
  return db.Text(strVal)
class Region(db.Model):

  dbPropStrRegionName = db.StringProperty()
  dbPropGPLocation = db.GeoPtProperty()
  dbPropStrLstTupleValues = db.StringListProperty()

  dbPropStrNowChartImg = db.StringProperty()
  dbPropStrWeeklyChartImg = db.StringProperty()
  dbPropEncBorder = db.TextProperty(default = "default")
  
class Nation(db.Model):
  dbPropStrLstTupleValues = db.StringListProperty()
  dbPropStrNowChartImg = db.StringProperty()
  dbPropStrWeeklyChartImg = db.StringProperty()
  
    
class TweetUtils(db.Model):

  dbPropIntLastId = db.IntegerProperty()
  

                      
                     
  
  

class ManageRegions():
  dictPropIndexes = { "regionName":"_dbPropStrRegionName",
                      "gpLocation": "_dbPropGPLocation",
                      "tupleValues": "_dbPropStrLstTupleValues",
                      "nowChart":"_dbPropStrNowChartImg",
                      "weeklyChart":"_dbPropStrWeeklyChartImg",
                      "borders":"_dbPropEncBorder"
                     }
  
  
  def InitRegions(self,lstInitVals):
    
    logging.info(lstInitVals)
    for itemOfLIV in lstInitVals:
      region = Region()  
      #logging.info(itemOfLOIV)
      for i in range(0,len(itemOfLIV)):
        region.__setattr__(self.dictPropIndexes[itemOfLIV[i][0]],itemOfLIV[i][1])
      region.put()
      
#  def InitRegionLists(self,lstInitVals):
#    query = db.GqlQuery("Select * FROM Region")
#    results = query.fetch(limit = None)
#    
#    for lstItemOfLIV in lstInitVals: #for each list of values in the list
#      for itemOfResults in results: #for each update value go through the db table looking for a matching region
#        if itemOfResults.dbPropStrRegionName == lstItemOfLIV[0]: 
#          
#          for i in range(1,len(lstItemOfLIV)):#now that we've found one extract the tuple values
#            for lstItem in lstItemOfLIV[i][1]:
#            #get the item to be updated from tuple item[i][0][indexes[0]:indexes[1]],values]
#              itemOfResults.__dict__[self.dictPropIndexes[lstItemOfLIV[i][0]]].append(lstItem)
#          itemOfResults.put()
#          break
       
  def CheckHasEntities(self):
    regions = Region.all()
    return regions.count(1) 
    
  def ReturnNumEntriesRegions(self):
    regions = Region.all()
    return regions.count() 
  
  #Schema for lstOfValues: [ [ "RegionName", ("KeyForValueToUpdate",value)....] ]
  def UpdateRegions(self,lstOfUpdateVals):
    query = db.GqlQuery("Select * FROM Region")
    results = query.fetch(limit = None)
    
    for itemOfLOUV in lstOfUpdateVals: #for each list of values in the list
      for itemOfResults in results: #for each update value go through the db table looking for a matching region
        if itemOfResults.dbPropStrRegionName == itemOfLOUV[0]:  
          for i in range(1,len(itemOfLOUV)):#now that we've found one extract the tuple values
            #eval("itemOfResults" + self.dictPropIndexes[itemOfLOUV[i][0]] + "=" + itemOfLOUV[i][1] )
            itemOfResults.__setattr__(self.dictPropIndexes[itemOfLOUV[i][0]],itemOfLOUV[i][1])
          itemOfResults.put()
          break
  
  #["County",("LstToUpdate",lstIndexes,values)]
  
  def tempRet(self):
    query = db.GqlQuery("Select * FROM Region")
    results = query.fetch(limit = 1)
    for item in results:
      return item
    
  
 
    
    
#  def UpdateRegionsLists(self,lstUpdateValues):
#    query = db.GqlQuery("Select * FROM Region")
#    results = query.fetch(limit = None)
#    
#    for lstItemOfLUV in lstUpdateVals: #for each list of values in the list
#      for itemOfResults in results: #for each update value go through the db table looking for a matching region
#        if itemOfResults.dbPropStrRegionName == lstItemOfLUV[0]: 
#          
#          for i in range(1,len(lstItemOfLUV)):#now that we've found one extract the tuple values
#            #get the item to be updated from tuple item[i][0][indexes[0]:indexes[1]],values]
#          
#            for indexItem in itemOfLOV[i][2]: #tuple contains a list of index lists
#              itemOfResults.__setattr__(self.dictPropIndexes[itemOfLOUV[i][0]][indexItem[0]:indexItem[1]],itemOfLOUV[i][1])
#          itemOfResults.put()
#          break
    
  

  def ReturnPropertyOfRegions(self, strKey):
    query = db.GqlQuery("Select * FROM Region")
    lstOfProp = []
    results = query.fetch(limit = None)
    for itemOfResults in results:
      lstOfProp.append( [itemOfResults.dbPropStrRegionName, itemOfResults.__dict__[self.dictPropIndexes[strKey]]] )
      #logging.info(itemOfResults.__dict__)
      
    return lstOfProp
  
  def ReturnAll(self):
    query = db.GqlQuery("Select * FROM Region")
    results = query.fetch(limit = None)
    logging.info(results)
    #results.sort(key = lambda name:self.dictPropIndexes["regionName"])
    return results
    
class ManageNation():
  
  dictPropIndexes = { 
                   "tupleValues": "_dbPropStrLstTupleValues",
                   "nowChart":"_dbPropStrNowChartImg",
                   "weeklyChart":"_dbPropStrWeeklyChartImg"}
  
  def InitNation (self,lstInitVals):
    nation = Nation()
    logging.info(lstInitVals)
    for itemOfLIV in lstInitVals: #'itemOFLIV is a list of tuples'
  
        nation.__setattr__(self.dictPropIndexes[itemOfLIV[0]],itemOfLIV[1])
        
        nation.put()
      
         
  def UpdateNation(self,lstOfUpdateVals):
    query = db.GqlQuery("Select * FROM Nation")
    results = query.fetch(limit = 1)
    
    for itemOfLOUV in lstOfUpdateVals: #for each list of values in the list
      for itemOfResults in results: #for each update value go through the db table looking for a matching region
         
        #for i in range(0,len(itemOfLOUV)):#now that we've found one extract the tuple values
        itemOfResults.__setattr__(self.dictPropIndexes[itemOfLOUV[0]],itemOfLOUV[1])
        itemOfResults.put()
       # break
  
  def ReturnPropertyOfNation(self, strKey):
      query = db.GqlQuery("Select * FROM Nation")
      lstOfProp = []
      results = query.fetch(limit = 1)
      for itemOfResults in results:
        return itemOfResults.__dict__[self.dictPropIndexes[strKey]]
  
  def ReturnAll(self):
    query = db.GqlQuery("Select * FROM Nation")
    results = query.fetch(limit = 1)
    
    return results  
  
  def CheckHasEntities(self):
    nation  = Nation.all()
    return nation.count(1)
  


class ManageTweetUtils():
  dictPropIndexes = { "lastid": "_dbPropIntLastId"
                     
                     } 
  
  def InitTweetUtils(self,lstOfInitValues):
     for itemOfLOIV in lstOfInitValues:
      tweetUtils = TweetUtils()
      logging.info(itemOfLOIV)
      for i in range(0,len(itemOfLOIV) -1):#now that we've found one extract the tuple values
        logging.info(itemOfLOIV)
        tweetUtils.__setattr__(self.dictPropIndexes[itemOfLOIV[i]], itemOfLOIV[i + 1])
      tweetUtils.put()
     
  def CheckHasEntities(self):
    tweetUtils = TweetUtils.all()
    return tweetUtils.count(1)
  
  def UpdateTweetUtils(self,lstOfUpdateValues):
    query = db.GqlQuery("Select * FROM TweetUtils")
    results = query.fetch(limit = None)
    
    for itemOfLOUV in lstOfUpdateValues:
      for itemOfResults in results:
        for i in range (len(itemOfLOUV)-1):#now that we've found one extract the tuple values
          itemOfResults.__setattr__(self.dictPropIndexes[itemOfLOUV[i]],itemOfLOUV[i + 1])
        itemOfResults.put()
        
  def ReturnPropertyOfTweetUtils(self, strKey):
    query = db.GqlQuery("Select * FROM TweetUtils")
    
    results = query.fetch(limit = None)
    returnedItem = None
    for itemOfResults in results:
      returnedItem = itemOfResults.__dict__[self.dictPropIndexes[strKey]]
      
    return returnedItem   
        
  def Clean(self):
    x = 5
#    query = db.GqlQuery("Select * FROM Nation")
#    results = query.fetch(limit = 1)
#    for item in results:
#      item.delattr       
      
      
      

